/// @Classname : HcTitle
/// @Author :
/// @Time : 2023-05-12 16:35
/// @Description : //todo

import 'package:flutter/material.dart';
import 'package:hc_ui/config/hc_customer_config.dart';
import 'package:hc_ui/theme/hc_title_theme.dart';
import 'package:hc_ui/utils/hc_string_util.dart';

class HcTitle extends StatelessWidget {
  //标题
  final String? title;

  //副标题
  final String? subtitle;

  //左侧Icon
  final Widget? leading;

  //副标题是否首字母大写
  final bool subTitleCapitalize;

  //右侧更多的 组件
  final Widget? trailing;

  final bool isShowDot;

  // 展示顶部圆形
  final bool isShowTopDot;

//  标题展示下划线
  final bool isShowUnderLine;

  //副标题堆叠效果
  final bool isStackSubtitle;

  // 标题最大显示行数
  final int titleMaxLines;

  // 副标题最大展示行数
  final int subtitleMaxLines;

  //快速设置的一些配置参数 如果设置了ThemeData 重合的 将会被覆盖掉
  final double? titleFontSize;
  final double? subtitleFontSize;
  final Color? backgroundColor;

  //标题的类型
  final HcTitleTheme? themeData;

  const HcTitle(
      {Key? key,
      this.title,
      this.subtitle,
      this.leading,
      this.subTitleCapitalize = true,
      this.trailing,
      this.themeData,
      this.isShowTopDot = false,
      this.isShowUnderLine = false,
      this.isStackSubtitle = false,
      this.titleMaxLines = 2,
      this.subtitleMaxLines = 2,
      this.isShowDot = false,
      this.titleFontSize,
      this.subtitleFontSize,
      this.backgroundColor})
      : super(key: key);

  const HcTitle.dot(
      {this.title,
      this.subtitle,
      this.subTitleCapitalize = true,
      super.key,
      this.trailing,
      this.themeData,
      this.titleMaxLines = 2,
      this.subtitleMaxLines = 2,
      this.titleFontSize,
      this.subtitleFontSize,
      this.backgroundColor})
      : leading = null,
        isShowDot = true,
        isShowTopDot = false,
        isShowUnderLine = false,
        isStackSubtitle = false;

  @override
  Widget build(BuildContext context) {
    HcTitleTheme theme = themeData ?? HcTitleTheme();
    theme = HcCustomerConfig().allTheme.titleTheme.merge(HcTitleTheme(
            backgroundColor: backgroundColor,
            titleStyle: TextStyle(fontSize: titleFontSize),
            subtitleStyle: TextStyle(fontSize: subtitleFontSize))
        .merge(theme));
    MainAxisAlignment alignment = theme.alignment;
    switch (theme.alignment) {
      case MainAxisAlignment.end:
        alignment = MainAxisAlignment.end;
        break;
      case MainAxisAlignment.center:
        alignment = MainAxisAlignment.center;
        break;
      default:
        alignment = MainAxisAlignment.start;
        break;
    }
    //副标题要不要首字母大写
    String? finalSubtitle = subtitle != null
        ? subTitleCapitalize
            ? HcStringUtil.capitalizeStr(subtitle)
            : subtitle
        : null;

    return Container(
      margin: theme.margin,
      padding: theme.widgetPadding,
      color: theme.backgroundColor,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Expanded(
              child: Row(
            mainAxisAlignment: alignment,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              if (leading != null)
                IconTheme(
                  data: IconThemeData(
                      color: theme.leadingColor, size: theme.leadingSize),
                  child: leading!,
                )
              else if (isShowDot)
                Container(
                  width: 5,
                  height: theme.titleStyle.fontSize!,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(5),
                    color: theme.dotColor,
                  ),
                ),
              if (title != null)
                Flexible(
                    child: Container(
                  margin: theme.titleMargin,
                  child: Stack(
                    clipBehavior: Clip.none,
                    children: [
                      if (isShowUnderLine)
                        Positioned(
                            left: theme.underlineStartIndent,
                            right: theme.underlineEndIndent,
                            bottom: 0,
                            child: Opacity(
                              opacity: theme.underlineOpacity,
                              child: Container(
                                height: theme.titleStyle.fontSize! / 2,
                                color: theme.underlineColor,
                              ),
                            )),
                      if (isShowTopDot)
                        Positioned(
                            left: -0.5 * theme.titleStyle.fontSize!,
                            top: -0.3 * theme.titleStyle.fontSize!,
                            child: Opacity(
                              opacity: theme.dotOpacity,
                              child: Container(
                                height: theme.titleStyle.fontSize!,
                                width: theme.titleStyle.fontSize!,
                                decoration: BoxDecoration(
                                    borderRadius: BorderRadius.circular(
                                        theme.leadingSize / 2),
                                    color: theme.dotColor),
                              ),
                            )),
                      if (isStackSubtitle && finalSubtitle != null)
                        Positioned(
                            top: -theme.subtitleStyle.fontSize!,
                            left: 0,
                            right: 0,
                            bottom: 0,
                            child: Opacity(
                                opacity: theme.stackSubtitleOpacity,
                                child: Text(
                                  finalSubtitle,
                                  maxLines: subtitleMaxLines,
                                  style: theme.stackSubtitleStyle,
                                  textAlign: TextAlign.end,
                                ))),
                      Text(
                        title ?? '',
                        maxLines: titleMaxLines,
                        overflow: TextOverflow.ellipsis,
                        style: theme.titleStyle,
                      )
                    ],
                  ),
                )),
              if (!isStackSubtitle && finalSubtitle != null)
                Container(
                  constraints: BoxConstraints(
                      maxWidth: title != null
                          ? MediaQuery.of(context).size.width / 4
                          : double.infinity),
                  margin: theme.subtitleMargin,
                  child: Text(
                    finalSubtitle,
                    style: theme.subtitleStyle,
                    overflow: TextOverflow.ellipsis,
                    maxLines: subtitleMaxLines,
                  ),
                ),
            ],
          )),
          if (trailing != null)
            Container(
              margin: theme.trailingMargin,
              child: IconTheme(
                  data: IconThemeData(
                      color: theme.trailingColor, size: theme.trailingSize),
                  child: trailing!),
            )
        ],
      ),
    );
  }
}
